package com.example.app3

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    private var tvName: TextView? = null
    private var tvGender: TextView? = null
    private var tvPhone: TextView? = null
    private var tvHighScore: TextView? = null
    private var btnEdit: Button? = null
    private var btnGame: Button? = null
    private var player: Player? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        tvName = findViewById<TextView?>(R.id.name)
        tvGender = findViewById<TextView?>(R.id.gender)
        tvPhone = findViewById<TextView?>(R.id.phone)
        tvHighScore = findViewById<TextView?>(R.id.high_score)
        btnEdit = findViewById<Button?>(R.id.btn_edit)
        btnGame = findViewById<Button?>(R.id.btn_game)

        player = Player("张三", "男", "13800138000", 0)
        updatePlayerInfo()

        btnEdit!!.setOnClickListener(View.OnClickListener { v: View? ->
            val intent = Intent(this@MainActivity, EditInfoActivity::class.java)
            intent.putExtra("name", player!!.getName())
            intent.putExtra("gender", player!!.getGender())
            intent.putExtra("phone", player!!.getPhone())
            startActivityForResult(intent, EDIT_INFO_REQUEST)
        })

        btnGame!!.setOnClickListener(View.OnClickListener { v: View? ->
            val intent = Intent(this@MainActivity, CardGameActivity::class.java)
            intent.putExtra("current_score", player!!.getHighScore())
            startActivityForResult(intent, 2)
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == EDIT_INFO_REQUEST && resultCode == RESULT_OK) {
            if (data != null) {
                player!!.setName(data.getStringExtra("name"))
                player!!.setGender(data.getStringExtra("gender"))
                player!!.setPhone(data.getStringExtra("phone"))
                updatePlayerInfo()
            }
        } else if (requestCode == 2 && resultCode == RESULT_OK) {
            if (data != null) {
                val newScore = data.getIntExtra("score", 0)
                if (newScore < player!!.getHighScore() || player!!.getHighScore() == 0) {
                    player!!.setHighScore(newScore)
                    updatePlayerInfo()
                }
            }
        }
    }

    private fun updatePlayerInfo() {
        tvName!!.setText(player!!.getName())
        tvGender!!.setText(player!!.getGender())
        tvPhone!!.setText(player!!.getPhone())
        tvHighScore!!.setText(player!!.getHighScore().toString())
    }

    companion object {
        private const val EDIT_INFO_REQUEST = 1
    }
}